package com.hadoop.mr.writable;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.lib.db.DBWritable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Administrator on 2019/7/11.
 */
public class ScoreWritable implements Writable, DBWritable {

    private String goodsName;
    private Integer goodsCount;
    private String goodsType;
    private String bestScore;
    private String worstScore;
    private String avgScore;
    private Double goodRate;

    public ScoreWritable() {
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public Integer getGoodsCount() {
        return goodsCount;
    }

    public void setGoodsCount(Integer goodsCount) {
        this.goodsCount = goodsCount;
    }

    public Double getGoodRate() {
        return goodRate;
    }

    public void setGoodRate(Double goodRate) {
        this.goodRate = goodRate;
    }

    public String getAvgScore() {
        return avgScore;
    }

    public void setAvgScore(String avgScore) {
        this.avgScore = avgScore;
    }

    public String getGoodsType() {
        return goodsType;
    }

    public void setGoodsType(String goodsType) {
        this.goodsType = goodsType;
    }

    public String getBestScore() {
        return bestScore;
    }

    public void setBestScore(String bestScore) {
        this.bestScore = bestScore;
    }

    public String getWorstScore() {
        return worstScore;
    }

    public void setWorstScore(String worstScore) {
        this.worstScore = worstScore;
    }

    public ScoreWritable(String goodsName, String goodsType, String bestScore, String worstScore, String avgScore, Integer goodsCount, Double goodRate) {
        this.goodsName = goodsName;
        this.goodsType = goodsType;
        this.bestScore = bestScore;
        this.worstScore = worstScore;
        this.avgScore = avgScore;
        this.goodsCount = goodsCount;
        this.goodRate = goodRate;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(goodsName);
        out.writeUTF(goodsType);
        out.writeUTF(bestScore);
        out.writeUTF(worstScore);
        out.writeUTF(avgScore);
        out.writeInt(goodsCount);
        out.writeDouble(goodRate);
    }


    @Override
    public void readFields(DataInput in) throws IOException {
        goodsName = in.readUTF();
        goodsType = in.readUTF();
        bestScore = in.readUTF();
        worstScore = in.readUTF();
        avgScore = in.readUTF();
        goodsCount = in.readInt();
        goodRate = in.readDouble();
    }

    @Override
    public void write(PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setString(1, this.goodsName);
        preparedStatement.setString(2, this.goodsType);
        preparedStatement.setString(3, this.bestScore);
        preparedStatement.setString(4, this.worstScore);
        preparedStatement.setString(5, this.avgScore);
        preparedStatement.setDouble(6, this.goodRate);
    }

    @Override
    public void readFields(ResultSet resultSet) throws SQLException {
        this.goodsName = resultSet.getString(1);
        this.goodsType = resultSet.getString(2);
        this.bestScore = resultSet.getString(3);
        this.worstScore = resultSet.getString(4);
        this.avgScore = resultSet.getString(5);
        this.goodRate = resultSet.getDouble(6);
    }

}

